﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsAppTest.Dao;

namespace WinFormsAppTest
{
    public partial class FormRegister : Form
    {
        public FormRegister()
        {
            InitializeComponent();
        }

        private void FormRegisterClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }

        private void Register_Click(object sender, EventArgs e)
        {
            TestDao registerdao = new TestDao();
            AdminDao admindao = new AdminDao();
            WorkDao workdao = new WorkDao();
            //  string.IsNullOrWhiteSpace是 判断是否含有一个或者多个空格的情况
            String username = NametextBox.Text;
            String password = PasswordtextBox.Text;
            if (string.IsNullOrWhiteSpace(username))
            {
                MessageBox.Show("注册账号不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("注册密码不能为空！");
            }
            else if (admindao.GetUserByPassword(password).Count != 0)
            {
                MessageBox.Show("注册密码已使用！");
            }
            else if (admindao.GetUserByName(username).Count != 0)
            {
                MessageBox.Show("用户名已使用！");
            }
            else
            {
                registerdao.CreateUser(username, password);
                workdao.CreateWork(admindao.GetUserByName(username)[0].Userid);
                MessageBox.Show("注册成功！");
                this.Hide();
                Form1 f = new Form1();
                f.ShowDialog();
                this.Dispose();
                return;
            }
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form1 f = new Form1();
            f.ShowDialog();
            this.Dispose();
        }
    }
}
